package com.wedo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import tk.mybatis.spring.annotation.MapperScan;

import java.time.Duration;

@SpringBootApplication
@EnableDiscoveryClient
@MapperScan("com.wedo.book.mapper")
@EnableCaching
public class WedoBookApplication {

    @Value("${spring.redis.ttl}")
    private String ttl;

    @Bean
    public RedisCacheConfiguration redisCacheConfiguration() {
        RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig();
        configuration = configuration.serializeValuesWith(RedisSerializationContext.
                SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())).entryTtl(Duration.ofDays(Long.valueOf(ttl)));;
        return configuration;
    }

    public static void main(String[] args) {
        SpringApplication.run(WedoBookApplication.class);
    }
}
